/** @namespace app.model.User */
module.exports = app => {
    const {STRING, INTEGER, DATE, VIRTUAL, BOOLEAN} = app.Sequelize;

    const User = app.model.define('user', {
        username: {
            type: STRING(30),
            unique: 'username',
            allowNull: false,
            validate: {
                notEmpty: true,
                len: [2, 30],
            }
        },
        phone: {
            type: STRING(11),
            unique: 'phone',
            allowNull: false,
            validate: {
                isNumeric: true,
                notEmpty: true,
                len: 11,
            }
        },
        email: {
            type: STRING,
            unique: 'email',
            allowNull: false,
            validate: {
                isEmail: true,
                notEmpty: true,
                len: [6, 255]
            }
        },
        password: {
            type: STRING,
            allowNull: false
        },
        last_sign_in_at: DATE,
        disable: DATE,
        message: {type: BOOLEAN, defaultValue: false},
    }, {
        updatedAt: false,
        defaultScope: {
            where: {
                id: {[app.Sequelize.Op.gt]: 1}
            },
            attributes: {exclude: ['password']},
        },
        scopes: {
            password: {
                attributes: {exclude: []},
            }
        },
    });

    User.associate = () => {
        const {
            User, Message, Project, UserMessage, UserProject, UserLevel,
            Report, ReportComment, DetailAnchor, DetailAnchorAgent
        } = app.model;

        //==========
        //用户级别
        User.belongsTo(UserLevel, {as: 'level'});

        //==========
        //用户的所有项目
        User.belongsToMany(Project, {as: 'project', through: {model: UserProject}});
        Project.belongsToMany(User, {as: 'user', through: {model: UserProject}});
        Project.belongsToMany(User, {
            as: 'emailUser',
            through: {
                model: UserProject,
                scope: {
                    email: true,
                }
            }
        });

        //==========
        //项目负责人
        User.belongsToMany(Project, {as: 'projectManager', through: {model: UserProject, scope: {type: 0}}});
        Project.belongsToMany(User, {as: 'manager', through: {model: UserProject, scope: {type: 0}}});

        //==========
        //项目执行
        User.belongsToMany(Project, {as: 'projectMember', through: {model: UserProject, scope: {type: 1}}});
        Project.belongsToMany(User, {as: 'member', through: {model: UserProject, scope: {type: 1}}});

        //==========
        //项目客户
        User.belongsToMany(Project, {as: 'projectClient', through: {model: UserProject, scope: {type: 2}}});
        Project.belongsToMany(User, {as: 'client', through: {model: UserProject, scope: {type: 2}}});

        //==========
        //主播的经纪人
        User.hasMany(DetailAnchor, {as: 'anchor', foreignKey: 'agent_id'});
        DetailAnchor.belongsTo(User, {as: 'agent', foreignKey: 'agent_id'});

        //==========
        //用户作为短消息发送者
        Message.belongsTo(User, {as: 'from', foreignKey: 'from_id'});
        User.hasMany(Message, {as: 'send', foreignKey: 'from_id'});

        //==========
        //用户作为短消息接收者
        User.belongsToMany(Message.scope('message'), {
            as: 'myMessage',
            through: UserMessage,
        });
        Message.belongsToMany(User, {
            as: 'receiver', through: UserMessage,
        });

        UserMessage.belongsTo(User);
        UserMessage.belongsTo(Message);

        //==========
        //用户的未读消息
        User.belongsToMany(Message, {as: 'unreadMessage', through: {model: UserMessage, scope: {read: false}}});

        //==========
        //项目 日报
        Project.hasMany(Report);
        Report.belongsTo(Project);

        //==========
        //日报 日报评论
        Report.hasMany(ReportComment, {as: 'comment'});
        ReportComment.belongsTo(Report);

        //==========
        //用户 日报评论
        User.hasMany(ReportComment);
        ReportComment.belongsTo(User);
    };

    return User;
};